<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="场地名字" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['name', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]"
          />
        </a-form-item>
        <a-form-item label="时间段模板" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            v-decorator="['time_template_id', { rules: [{ required: true, message: '请选择一个时间段模板!' }] }]"
            placeholder="请选择一个时间段模板"
          >
            <template v-for="template in templateList">
              <a-select-option :value="template.id" :key="template.id"> {{template.name}} </a-select-option>
            </template>
          </a-select>
        </a-form-item>
        <a-form-item label="最少选择" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="限制该场地最少需要预约多少场，0为无限制">
          <a-input-number
            :min="0"
            v-decorator="['minimum', { initialValue: 0, rules: [{ required: true, message: '请输入至少1个数字' }, { validator: limitValidator, message: '最多选择限制必须大于等于最少选择限制' },] }]"
          />
        </a-form-item>
        <a-form-item label="最多选择" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="限制该场地最多只能预约多少场，0为无限制">
          <a-input-number
            :min="0"
            v-decorator="['maximum', { initialValue: 0, rules: [{ required: true, message: '请输入至少1个数字' }, { validator: limitValidator, message: '最多选择限制必须大于等于最少选择限制' },] }]"
          />
        </a-form-item>
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            v-decorator="['status', { rules: [{ required: true, message: '请选择一个状态!' }] }]"
          >
            <a-select-option :value="0"> 开启 </a-select-option>
            <a-select-option :value="1"> 关闭 </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="数字越小越靠前">
          <a-input-number
            :min="0"
            v-decorator="['sort', { initialValue: 10, rules: [{ required: true, message: '请输入至少1个数字' }] }]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as Api from '@/api/arena/index'
import * as templateApi from '@/api/time/template'

export default {
  props: {},
  data() {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 模板列表
      templateList: [],
    }
  },
  methods: {
    limitValidator: function (rule, value, callback) {
      const { minimum, maximum } = this.form.getFieldsValue(['minimum', 'maximum'])
      if((minimum > maximum) && maximum != 0) {
        callback('最多选择限制必须大于等于最少选择限制')
      }
      callback()
    },
    /**
     * 显示对话框
     */
    add() {
      this.title = '新增场地'
      this.visible = true

      // 设置可选择的模板
      this.setTimeTempalte()
    },

    // 设置可选择的模板
    setTimeTempalte(){
      templateApi.list().then(response => {
        console.log(response)
        this.templateList = response.data.list.data.map(template => {
          return {
            id: template.time_template_id,
            name: template.name
          }
        })
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      const {
        form: { validateFields },
      } = this
      // 表单验证
      validateFields((errors, values) => {
        !errors && this.onFormSubmit(values)
      })
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.add(values)
        .then((result) => {
           // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    }
  },
}
</script>
